<?php
/**
 * Created by PhpStorm.
 * User: mac
 * Date: 2015/4/4
 * Time: 15:08
 */

namespace Common\Model;
use Think\Model;

class ContentModel extends Model{

    /**
     * 获取文章树形数组
     * @param int:父级id
     * @return array
     */
    public function getArticleNodeList($parent_id=0,$current_id=0)
    {
        $model = M('Category');
        $node = null;
        $map['parent_id'] = $parent_id;
        $articleList = $model->where($map)->order("node_order DESC,node_id ASC")->select();
        if($articleList)
        {
            foreach($articleList AS $key=>$val)
            {

                $iconCls = 'icon_txt';      //节点图标
                $children = $this->getArticleNodeList($val['node_id'],$current_id);
                if($children)
                {
                    $state = 'closed';
                    if($current_id > 0 && $this->isParentCate($val['cat_id'],$current_id) || $val['parent_id']==0) $state = 'open';
                    $node[] = array('id'=>$val['node_id'],'text'=>$val['node_name'],'state'=>$state,'children'=>$children,'parent'=>$val['parent_id'],'iconCls'=>$iconCls);
                }
                else
                {
                    $node[] = array('id'=>$val['node_id'],'text'=>$val['node_name'],'parent'=>$val['parent_id'],'iconCls'=>$iconCls);
                }

            }
        }
        return $node;
    }



    /**
     * 检查一个id是否是另一个id的父id
     * @param $pid
     * @param $id
     * @return bool
     */
    public function isParentCate($pid,$id)
    {
        $flag = false;
        $parent_id = $this->where("`node_id`={$id}")->getField('parent_id');
        if($parent_id > 0)
        {
            if($parent_id == $pid)
            {
                $flag = true;
            }
            else
            {
                return $this->isParentCate($pid,$parent_id);
            }
        }
        return $flag;
    }


} 